package com.zjitc.pigstudy.service;

import com.zjitc.pigstudy.Exception.ServiceException;
import com.zjitc.pigstudy.domain.Time;
import com.zjitc.pigstudy.domain.User;
import com.zjitc.pigstudy.domain.UserDTO;
import com.zjitc.pigstudy.mapper.TimeMapper;
import com.zjitc.pigstudy.mapper.UserMapper;
import com.zjitc.pigstudy.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TimeMapper timeMapper;

    /**
     * 登录功能
     * @param user
     * @return
     */
    @Override
    public User login(User user) {

        User dbuser = userMapper.UserByName(user.getAccount());
        if(dbuser == null){
            throw new ServiceException("用户名或密码错误");
        }
        if(!dbuser.getPassword().equals(user.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }


        //生成token
        String token = TokenUtils.getToken(dbuser.getId().toString(),user.getPassword());
        dbuser.setToken(token);
        return dbuser;
    }

    /**
     * 注册功能
     * @param user
     * @return
     */
    @Override
    public User register(User user) {
        User dbuser = userMapper.UserByName(user.getAccount());
        if(dbuser!=null){
            throw new ServiceException("账号已存在");
        }
        Boolean flag = userMapper.register(user);
        if(!flag){
            throw new ServiceException("注册失败");
        }

        return user;
    }

    /**
     * 获取表格数据
     * @return
     */
    @Override
    public List<User> selectAll() {
        List<User> user = userMapper.selectAll();
        if(user == null){
            throw new ServiceException("用户目录刷新失败");
        }
        return user;
    }

    /**
     * 修改用户信息
     * @param user
     */
    @Override
    public void updateUser(User user) {
        boolean flag = userMapper.updateUser(user);
        if(!flag){
            throw new ServiceException("修改失败");
        }

    }

    /**
     * 重置密码
     * @param user
     */
    @Override
    public void resetPassword(User user) {
        System.out.println(user.getAccount());
        User dbuser = userMapper.UserByName(user.getAccount());
        if(dbuser==null){
            throw new ServiceException("账号不存在");
        }
        if(!dbuser.getPhone().equals(user.getPhone())){
            throw new ServiceException("手机号输入有误");
        }
        //重置密码

        dbuser.setPassword("123");
        userMapper.updateUser(dbuser);

    }

    /**
     * 获取上次登录时间
     * @param id
     * @return
     */
    @Override
    public Time getLoginTime(Integer id) {
        Time time = timeMapper.getLoginTime(id);
        if(time ==null){
            throw new ServiceException("获取登录时间失败");
        }
        return time;
    }

    /**
     * 设置登录时间
     *
     * @param time
     */
    @Override
    public void setLoginTime(Time time) {
       boolean flag = timeMapper.setLoginTime(time);
       if (!flag){
           throw new ServiceException("设置失败");
       }

    }
}
